<template>
  <!-- 登录页面 -->
  <div class="container-login">
    <div class="box-login">
      <div class="box-login-title"></div>
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginRef">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            clearable
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            clearable
            placeholder="请输入密码"
            v-model="loginForm.password"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-button class="login-all" type="primary" @click="loginall">登录</el-button>
        <el-link type="primary">去注册</el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api/uers.js'
export default {
  name: 'login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '数字1-10', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '数字1-10', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginall() {
      this.$refs.loginRef.validate(async value => {
        if (!value) return
        console.log('验证成功')
        const { data: res } = await loginAPI(this.loginForm)
        if (res.code === 1) {
          this.$message.error(res.message)
        } else {
          // 弹框
          this.$message.success(res.message)
          // 将token值存到vuex中
          // 格式 this.$store.commit('模块注册名/mutation中方法名', 参数)
          this.$store.commit('uers/zhtoken', res.token)
          // 跳转页面
          // 格式  this.$router.push('/路径') 或this.$router.push({name:'路由规则名'})
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.container-login {
  height: 100%;
  background: url('./../../assets/images/login_bg.jpg'), no-repeat;
  background-size: cover;
  .box-login {
    width: 400px;
    height: 300px;
    background: white;
    border-radius: 20px;
    position: absolute;
    padding: 0px 20px;
    box-sizing: border-box;
    top: 50%;
    left: 50%;
    margin-top: -200px;
    margin-left: -167.5px;
    .box-login-title {
      height: 80px;
      background: url('./../../assets/images/login_title.png') no-repeat;
      background-position: center;
    }

    .login-all {
      width: 100%;
    }
  }
}
</style>
